﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using CM.DL.DALC.NC03;
using DECOR.BL.BE;

namespace DECOR.DL.DALC.NC03
{
    public class JerarquiaDALC : IJerarquiaDALC
    {
        private string _connId = string.Empty;

        public static JerarquiaDALC Instance(string pConnId)
        {
            if (_instance == null)
            {
                lock (typeof(JerarquiaDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new JerarquiaDALC(pConnId);
                    }
                }
            }
            return _instance;
        }
        private JerarquiaDALC(string pConnId) { this._connId = pConnId; }
        private JerarquiaDALC() { }
        private static volatile JerarquiaDALC _instance = null;
        #region IJerarquiaDALC Members

        public List<DECOR.BL.BE.JerarquiaBE> listar(int nivel)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            DestConfig dc = new DestConfig();
            JerarquiaBE obj = null;
            List<JerarquiaBE> listaResult = new List<JerarquiaBE>();
            try
            {
                //try
                //{
                //    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                //}
                //catch { }
                //RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_LISTA_JERARQUIAS");
                function.SetValue("IM_STUFE", nivel.ToString());
                function.Invoke(dest);

                IRfcTable T179 = function.GetTable("T_T179");

                foreach (IRfcStructure item in T179)
                {
                    obj = new JerarquiaBE();

                    obj.Jerarquia = item["PRODH"].GetString();
                    obj.Descripcion = item["VTEXT"].GetString();
                    obj.Nivel = Convert.ToInt32(item["STUFE"].GetString());

                    listaResult.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return listaResult;
        }

        public List<DECOR.BL.BE.JerarquiaBE> Buscar(int pNivel, string pJerarquia)
        {
            IRfcFunction function = null; RfcDestination dest = null;
            //InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            DestConfig dc = new DestConfig();
            JerarquiaBE obj = null;
            List<JerarquiaBE> listaResult = new List<JerarquiaBE>();
            try
            {
                //try
                //{
                //    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                //}
                //catch { }
                //RfcDestination dest = RfcDestinationManager.GetDestination(this._connId);
                dest = DALC.getDestination(out dc);
                function = dest.Repository.CreateFunction("ZWEB_LISTA_JERARQUIAS");
                function.SetValue("IM_STUFE", pNivel.ToString());
                function.Invoke(dest);

                IRfcTable T179 = function.GetTable("T_T179");

                foreach (IRfcStructure item in T179)
                {
                    obj = new JerarquiaBE();

                    obj.Jerarquia = item["PRODH"].GetString();
                    obj.Descripcion = item["VTEXT"].GetString();
                    obj.Nivel = Convert.ToInt32(item["STUFE"].GetString());

                    listaResult.Add(obj);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return listaResult;
        }

        #endregion
    }
}
